package com.yuan.pdftest.test.pdf.hg;

import com.yuan.pdftest.test.pdf.PDFTemplateUtil;
import com.yuan.pdftest.test.pdf.domain.PdfDataTest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class cg {
    @RequestMapping("/e")
    public void exportPdf2(HttpServletResponse response) throws Exception {
        Map<String, Object> data = getStringObjectMap();
        String fileName = URLEncoder.encode("xxx报告.doc", "UTF-8");
        try {
            PdfPrintUntils.exportPdfByFreeMarkerss( "pdf测试模板.ftl",data, fileName, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("生成pdf失败：" + e.getMessage());
        }
    }
    private Map<String, Object> getStringObjectMap() {
        // 模板中的数据，实际运用从数据库中查询
        Map<String, Object> data = new HashMap<>();
        data.put("curr", 1);
        data.put("one", 2);
        data.put("two", 1);
        data.put("three", 6);
        List<PdfDataTest> detailList = new ArrayList<>();
        detailList.add(new PdfDataTest(123456, "测试", "测试", "测试", "测试"));
        detailList.add(new PdfDataTest(111111, "测试shi", "测试", "测试", "测试"));
        detailList.add(new PdfDataTest(222222, "测试", "测试", "测试", "测试"));
        data.put("detailList", detailList);
        return data;
    }

}
